package leetcode.每日一题;

import leetcode.util.CommonUtils;
import leetcode.剑指offer.pojo.TreeNode;
import org.junit.Test;

/**
 * @author ：zsy
 * @date ：Created 2022/10/10 8:17 PM
 * @description：
 */
public class 将有序数组转换为二叉搜索树 {

    @Test
    public void test() {
        Solution solution = new Solution();
        TreeNode root = solution.sortedArrayToBST(new int[]{-10, -3, 0, 5, 9, 10, 25, 40, 46, 60, 80, 90, 100, 120, 130, 140, 150});
        CommonUtils.printTreeNodeByLayer(root);
    }

    class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            return constructNode(nums, 0, nums.length);
        }

        private TreeNode constructNode(int[] nums, int l, int r) {
            if (l >= r) return null;
            int mid = l + r >> 1;
            TreeNode node = new TreeNode(nums[mid]);
            node.left = constructNode(nums, l, mid);
            node.right = constructNode(nums, mid + 1, r);
            return node;
        }
    }
}

